package com.sign.common.util.string;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Objects.isNull;

public class StringUtil
{
  public static final String EMPTY_STR = "";

  public static boolean isNullOrEmpty(String str)
  {
    return (str == null) || ("".equals(str));
  }

  public static boolean isNotNullOrEmpty(String str) {
    return !isNullOrEmpty(str);
  }

  public static String padRight(String str, int totalWidth, char paddingChar) {
    if (isNullOrEmpty(str)) {
      str = "";
    }

    int strLen = str.length();
    if (strLen >= totalWidth) {
      return str;
    }

    StringBuilder sb = new StringBuilder(totalWidth);
    sb.append(str);
    while (sb.length() < totalWidth) {
      sb.append(paddingChar);
    }

    return sb.toString();
  }

  public static String padLeft(String str, int totalWidth, char paddingChar) {
    if (isNullOrEmpty(str)) {
      str = "";
    }

    int strLen = str.length();
    if (strLen >= totalWidth) {
      return str;
    }

    StringBuilder sb = new StringBuilder(totalWidth);
    int len = totalWidth - strLen;
    while (sb.length() < len) {
      sb.append(paddingChar);
    }
    sb.append(str);

    return sb.toString();
  }

  public static String trimEnd(String target, char c) {
    if ((isNullOrEmpty(target)) || (!target.endsWith(String.valueOf(c)))) {
      return target;
    }

    for (int i = target.length() - 1; i >= 0; i--) {
      if (target.charAt(i) == c) {
        if (i == 0) {
          return "";
        }
      }
      else {
        return target.substring(0, i + 1);
      }
    }
    return target;
  }

  public static String trimStart(String target, char c) {
    if ((isNullOrEmpty(target)) || (!target.startsWith(String.valueOf(c)))) {
      return target;
    }

    int len = target.length() - 1;
    for (int i = 0; i <= len; i++) {
      if (target.charAt(i) == c) {
        if (i == len) {
          return "";
        }
      }
      else {
        return target.substring(i);
      }
    }
    return target;
  }

  public static String generateUUID() {
    return UUID.randomUUID().toString().replace("-", "");
  }

  public static int getDefaultByteCount(String text) {
    if (isNullOrEmpty(text)) {
      return 0;
    }
    return text.getBytes().length;
  }

  public static String getErrorDetail(Exception e) {
    Writer w = null;
    PrintWriter pw = null;
    try {
      w = new StringWriter();
      pw = new PrintWriter(w);
      e.printStackTrace(pw);
      return w.toString();
    } catch (Exception localException1) {
    } finally {
      if (w != null) {
        try {
          w.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }

      if (pw != null) {
        pw.close();
      }
    }

    return "";
  }

  public static Boolean isEqual(String a, String b) {
    if ((a == null) && (b == null)) {
      return Boolean.valueOf(true);
    }

    if (a != null) {
      return Boolean.valueOf(a.equals(b));
    }

    return Boolean.valueOf(b.equals(a));
  }

  public static boolean isNullOrEmpty(Object obj) {
    return (obj == null) || ("".equals(String.valueOf(obj).trim()));
  }

  public static String[] spiltStringToArray(String str, String seprator) {
    List list = new ArrayList();
    if (isNullOrEmpty(str))
      return null;
    int index = str.indexOf(seprator);
    if (index >= 0) {
      while (index >= 0) {
        String tem = str.substring(0, index);
        list.add(tem);
        str = str.substring(index + 1);
        index = str.indexOf(seprator);
      }
      if (!isNullOrEmpty(str))
        list.add(str);
    }
    else {
      list.add(str);
    }
    String[] arr = new String[list.size()];
    for (int i = 0; i < list.size(); i++) {
      arr[i] = ((String)list.get(i));
    }
    return arr;
  }

  public static String underline2Camel(String line, boolean smallCamel)
  {
    if ((line == null) || ("".equals(line))) {
      return "";
    }
    StringBuffer sb = new StringBuffer();
    Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
      String word = matcher.group();
      sb.append((smallCamel) && (matcher.start() == 0) ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
      int index = word.lastIndexOf(95);
      if (index > 0)
        sb.append(word.substring(1, index).toLowerCase());
      else {
        sb.append(word.substring(1).toLowerCase());
      }
    }
    return sb.toString();
  }

  public static String camel2Underline(String line)
  {
    if ((line == null) || ("".equals(line))) {
      return "";
    }
    line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
    StringBuffer sb = new StringBuffer();
    Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
      String word = matcher.group();
      sb.append(word.toUpperCase());
      sb.append(matcher.end() == line.length() ? "" : "_");
    }
    return sb.toString().toLowerCase();
  }

  /**
   * * 判断一个字符串是否为空串
   *
   * @param str String
   * @return true：为空 false：非空
   */
  public static boolean isEmpty(String str)
  {
    return isNull(str) || EMPTY_STR.equals(str.trim());
  }

  /**
   * 截取字符串
   *
   * @param str 字符串
   * @param start 开始
   * @param end 结束
   * @return 结果
   */
  public static String substring(final String str, int start, int end)
  {
    if (str == null)
    {
      return EMPTY_STR;
    }

    if (end < 0)
    {
      end = str.length() + end;
    }
    if (start < 0)
    {
      start = str.length() + start;
    }

    if (end > str.length())
    {
      end = str.length();
    }

    if (start > end)
    {
      return EMPTY_STR;
    }

    if (start < 0)
    {
      start = 0;
    }
    if (end < 0)
    {
      end = 0;
    }

    return str.substring(start, end);
  }

  /**
   * * 判断一个对象是否非空
   *
   * @param object Object
   * @return true：非空 false：空
   */
  public static boolean isNotNull(Object object)
  {
    return !isNull(object);
  }
}